package com.skh.sort;

import java.util.Arrays;

/**
 * @Author: skh
 * @Date: 2019/6/17 16:23
 * @Description: 归并排序 nlogn
 */
public class MergeSort {

    public static void main(String[] args) {
        int[] arr = {1, 3, 2, 4, 6, 5};
        sort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] a, int low, int high) {
        if (low == high) {
            return;
        }

        int mid = (low + high) / 2;
        sort(a, low, mid);
        sort(a, mid + 1, high);
        //左右归并
        merge(a, low, mid, high);
    }

    public static void merge(int[] a, int low, int mid, int high) {
        int[] temp = new int[high - low + 1];
        int i = low;
        int j = mid + 1;
        int k = 0;
        // 把较小的数先移到新数组中
        while (i <= mid && j <= high) {
            if (a[i] <= a[j]) {
                temp[k++] = a[i++];
            } else {
                temp[k++] = a[j++];
            }
        }
        // 把左边剩余的数移入数组
        while (i <= mid) {
            temp[k++] = a[i++];
        }
        // 把右边边剩余的数移入数组
        while (j <= high) {
            temp[k++] = a[j++];
        }
        // 把新数组中的数覆盖nums数组
        for (int x = 0; x < temp.length; x++) {
            a[x + low] = temp[x];
        }

        System.out.println(low);
        System.out.println(mid);
        System.out.println(high);
        System.out.println(Arrays.toString(temp));

    }
}
